package l1j.server.server.model.poison;

import static l1j.server.server.model.skill.L1SkillId.EARTH_BIND;
import static l1j.server.server.model.skill.L1SkillId.FREEZING_BLIZZARD;
import static l1j.server.server.model.skill.L1SkillId.FREEZING_BREATH;
import static l1j.server.server.model.skill.L1SkillId.ICE_LANCE;
import static l1j.server.server.model.skill.L1SkillId.NPC_FREEZING_BREATH;
import static l1j.server.server.model.skill.L1SkillId.STATUS_POISON;


import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1PcInstance;

public class L1DamagePoison extends L1Poison {
	private class NormalPoisonTimer extends Thread {
		@Override
		public void run() {
			while (true) {
				try {
					Thread.sleep(L1DamagePoison.this._damageSpan);
				} catch (final InterruptedException e) {
					break;
				}

				if (!L1DamagePoison.this._target.hasSkillEffect(STATUS_POISON)) {
					break;
				}
				if (L1DamagePoison.this._target.hasSkillEffect(ICE_LANCE)) {
					break;
				}
				if (L1DamagePoison.this._target.hasSkillEffect(FREEZING_BLIZZARD)) {
					break;
				}
				if (L1DamagePoison.this._target.hasSkillEffect(FREEZING_BREATH)) {
					break;
				}
				if (L1DamagePoison.this._target.hasSkillEffect(EARTH_BIND)) {
					break;
				} if (L1DamagePoison.this._target.hasSkillEffect(NPC_FREEZING_BREATH)){
					break;
				}
				if (L1DamagePoison.this._target instanceof L1PcInstance) {
					final L1PcInstance player = (L1PcInstance) L1DamagePoison.this._target;
					player.receiveDamage(L1DamagePoison.this._attacker, L1DamagePoison.this._damage, false);
					if (player.isDead()) { // 死亡したら解毒処理
						break;
					}
				} else if (L1DamagePoison.this._target instanceof L1MonsterInstance) {
					final L1MonsterInstance mob = (L1MonsterInstance) L1DamagePoison.this._target;
					mob.receiveDamage(L1DamagePoison.this._attacker, L1DamagePoison.this._damage);
					if (mob.isDead()) { // 死亡しても解毒しない
						return;
					}
				}
			}
			L1DamagePoison.this.cure(); // 解毒処理
		}
	}
	public static boolean doInfection(final L1Character attacker, final L1Character cha,
			final int damageSpan, final int damage, final boolean can) {
		if (!isValidTarget(cha,can)) {//opqlo 毒性可否抗拒
			return false;
		}

		cha.setPoison(new L1DamagePoison(attacker, cha, damageSpan, damage));
		return true;
	}
	private Thread _timer;
	private final L1Character _attacker;
	private final L1Character _target;

	private final int _damageSpan;

	private final int _damage;

	private L1DamagePoison(final L1Character attacker, final L1Character cha,
			final int damageSpan, final int damage) {
		this._attacker = attacker;
		this._target = cha;
		this._damageSpan = damageSpan;
		this._damage = damage;

		this.doInfection();
	}

	@Override
	public void cure() {
		if (this._timer != null) {
			this._timer.interrupt(); // 毒タイマー解除
		}

		this._target.setPoisonEffect(0);
		this._target.killSkillEffectTimer(STATUS_POISON);
		this._target.setPoison(null);
	}

	private void doInfection() {
		this._target.setSkillEffect(STATUS_POISON, 30000);
		this._target.setPoisonEffect(1);

		if (this.isDamageTarget(this._target)) {
			this._timer = new NormalPoisonTimer();
			GeneralThreadPool.getInstance().execute(this._timer); // 通常毒タイマー開始
		}
	}

	@Override
	public int getEffectId() {
		return 1;
	}

	boolean isDamageTarget(final L1Character cha) {
		return cha instanceof L1PcInstance
				|| cha instanceof L1MonsterInstance;
	}
}
